from rest_framework.authentication import BaseAuthentication
from rest_framework_jwt.utils import jwt_decode_handler

from users.models import User


class JWTAuth(BaseAuthentication):
    """自定义认证"""
    def authenticate(self, request):
        token = request.META.get('HTTP_AUTHORIZATION')
        if token:
            payload = jwt_decode_handler(token[7:])
            # user = User(**payload)
            user = User.objects.get(id=payload['user_id'])
        else:
            user = None
        return user, token[7:]